/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ace.states;

import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import ace.game.entity.player.Profile;
import ace.game.entity.weapon.Weapon;
import ace.game.particle.ParticleEmitterFactory;
import ace.system.BitmapFont;
import ace.system.StateManager;
import ace.system.Utilities;
import ace.system.sound.SFXPlayer;

/**
 *
 * @author TimotiusNC
 */
public class StateHelp extends StateGame {
	protected static final long ANIMATION_DELAY = 100;

	public StateHelp(){
		super(-2, new Profile());
        m_Background = StateManager.instance().getBackgroundImage();

		//Initialize
		m_Time		= 0;
		m_Selected	= 0;
		m_Drawn		= 0; //Drawn 0 times (karena entah kenapa untuk StateHelp ini, draw duluan baru update
		m_DrawBG	= true;
		m_Pages		= new Image[4];

        // initialize profile
        mProfile = new Profile();

        // animating tutorial stuffs
        mParticleEmitters = new Vector();
		
        // create deleter
		mDeleteEmitters		= new Vector();
		mDeleteEnemies		= new Vector();
        mDeletePickups      = new Vector();
			
		try {
			//Load images
			mProfileBoard       = Image.createImage(Utilities.INTERFACE_FOLDER + "/profile_board.png");
            mHealthBar          = Image.createImage(Utilities.INTERFACE_FOLDER + "/health_bar.png");
            mLife               = Image.createImage(Utilities.PICKUP_FOLDER + "/life.png");
			m_Pages[0]			= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("1 / " + m_Pages.length, 0x00000000);
			m_Pages[1]			= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("2 / " + m_Pages.length, 0x00000000);
			m_Pages[2]			= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("3 / " + m_Pages.length, 0x00000000);
			m_Pages[3]			= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("4 / " + m_Pages.length, 0x00000000);

			//Tutorial 1
			m_BattleTexts			= new Image[6];
			m_BattleTexts[0]		= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Move your ship", 0x000000);
			m_BattleTexts[1]		= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("using the keypad.", 0x000000);
			m_BattleTexts[2]		= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(" ", 0x000000);
			m_BattleTexts[3]		= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Accumulate scores", 0x000000);
			m_BattleTexts[4]		= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("by killing the", 0x000000);
			m_BattleTexts[5]		= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("enemies.", 0x000000);

			m_PowerUpTexts			= new Image[7];
			m_PowerUpTexts[0]		= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Pick up power ups", 0x000000);
			m_PowerUpTexts[1]		= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("to help you finish", 0x000000);
			m_PowerUpTexts[2]		= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("the game:", 0x000000);
			m_PowerUpTexts[3]		= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(" ", 0x000000);
			m_PowerUpTexts[4]		= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Add total score", 0x000000);
			m_PowerUpTexts[5]		= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Replenish health", 0x000000);
			m_PowerUpTexts[6]		= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Gain life", 0x000000);

			m_PowerUpsImg			= new Image[3];
			m_PowerUpsImg[0]		= Image.createImage(Utilities.PICKUP_FOLDER + "/coin.png");
			m_PowerUpsImg[0]		= Image.createImage(m_PowerUpsImg[0], 0, 0, m_PowerUpsImg[0].getWidth()/6, m_PowerUpsImg[0].getHeight(), 0);
			m_PowerUpsImg[1]		= Image.createImage(Utilities.PICKUP_FOLDER + "/health.png");
			m_PowerUpsImg[1]		= Image.createImage(m_PowerUpsImg[1], 0, 0, m_PowerUpsImg[1].getWidth()/4, m_PowerUpsImg[1].getHeight(), 0);
			m_PowerUpsImg[2]		= Image.createImage(Utilities.PICKUP_FOLDER + "/life.png");

			//Tutorial 2
			m_MissionFinishedImg	= Image.createImage(Utilities.INTERFACE_FOLDER + "/tutorial1.png");
			m_MoneyTexts			= new Image[8];
			m_MoneyTexts[0]			= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Money earned on each", 0x000000);
			m_MoneyTexts[1]			= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("level is based on your", 0x000000);
			m_MoneyTexts[2]			= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("final score and your", 0x000000);
			m_MoneyTexts[3]			= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("performance.", 0x000000);
			m_MoneyTexts[4]			= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Higher performance", 0x000000);
			m_MoneyTexts[5]			= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("will result in higher", 0x000000);
			m_MoneyTexts[6]			= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("rank,thus resulting", 0x000000);
			m_MoneyTexts[7]			= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("in more money.", 0x000000);

			//Tutorial 3
			m_GarageMenuImg			= Image.createImage(Utilities.INTERFACE_FOLDER + "/tutorial2.png");
			m_GarageImg				= Image.createImage(Utilities.INTERFACE_FOLDER + "/tutorial3.png");
			m_GaragePartsTexts		= new Image[8];
			m_GaragePartsTexts[0]	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Upgrade your ship in", 0x000000);
			m_GaragePartsTexts[1]	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Garage. You may buy,", 0x000000);
			m_GaragePartsTexts[2]	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("sell, or assemble parts", 0x000000);
			m_GaragePartsTexts[3]	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("in Garage.", 0x000000);
			m_GaragePartsTexts[4]	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("More parts will be", 0x000000);
			m_GaragePartsTexts[5]	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("available by completing", 0x000000);
			m_GaragePartsTexts[6]	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("levels in certain", 0x000000);
			m_GaragePartsTexts[7]	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("condition.", 0x000000);

		} catch (IOException ex) {}

        initLevel();
	}

	public void keyPressed(int key){
		//Super
		super.keyPressed(key);

		if (key == -7 || key == 35) {
			StateManager.instance().goTo(GameState.FRONT, null, false);
			
			//Back sound effect StateManager.instance().playSFX("/cancel.wav");
			StateManager.instance().getSFXPlayer().playSFX(SFXPlayer.SFX_CANCEL);
		}
	}

    protected void initLevel() {
        // initialize level
        mCurrentLevel = m_Selected - 2;
        super.initLevel();

		mDrawEndX   = mDrawStartX + (StateManager.instance().getColumn() * Utilities.TILE_WIDTH) / 2;

        // initialize gameplay data
        mPlayerElapsedTime    = 0;

        // initialize player data
		mPlayer             = mProfile.buildPlayer();
		mPlayer.position.x  = (mDrawStartX + mDrawEndX) / 2;
		mPlayer.position.y  = mDrawStartY + 3 * StateManager.instance().getHeight() / 4;
    }

	public void update(long time, int keys) {
		if(m_Drawn <= 5)	return; //if canvas has not been drawn at least once, pass this update (so that any user input will be ignored

        //Reset arrow
        if (m_Time > 0) m_Time -= time;
        else {
            m_Time = 0;
        }

        int OldSelection = m_Selected;

        //Check keys
        if ((keys & GameCanvas.LEFT_PRESSED) != 0)	{
            //Previous
            m_Selected--;
			if(m_Selected < 0) m_Selected = TUTORIAL_STEPS-1;

            //Animate
            m_Time = ANIMATION_DELAY;

            //Cursor sound StateManager.instance().playSFX("/cursor.wav");
            StateManager.instance().getSFXPlayer().playSFX(SFXPlayer.SFX_CURSOR);
			m_DrawBG = true;
        }

        if ((keys & GameCanvas.RIGHT_PRESSED) != 0 || (keys & GameCanvas.FIRE_PRESSED) != 0)	{
            //Next
            m_Selected++;
			if(m_Selected >= TUTORIAL_STEPS) m_Selected = 0;

            //Animate
            m_Time = ANIMATION_DELAY;

            //Cursor sound StateManager.instance().playSFX("/cursor.wav");
            StateManager.instance().getSFXPlayer().playSFX(SFXPlayer.SFX_CURSOR);
			m_DrawBG = true;
        }

        //Correct selection
        if (m_Selected < 0) m_Selected = m_Pages.length - 1;
        m_Selected = m_Selected % m_Pages.length;

        //Create page number
        if (OldSelection != m_Selected) {
            // initialize scene based on selected page
            switch (m_Selected) {
                case 0:
                case 1:
                    initLevel();
                    break;
                case 2:
                    break;
                case 3:
                    break;
            }
        }
        switch (m_Selected) {
            case 0:
            case 1:
                super.update(time, 0);
                mKeysHoldable = false;
                checkScript(time);
                // scrolling to top
                break;
            case 2:
                // TODO: isi kene tjuk
                break;
            case 3:
                // TODO: isi kene tjuk
                break;
        }
	}

    private void checkScript(long time) {
        // hardcoded player pathing
        mPlayerElapsedTime += time;
        final float playerVelocity = mPlayer.getData().getVelocity() / 100.0f;
        if (m_Selected == 0) {
            if (mPlayerElapsedTime < 1000) {

            } else if (mPlayerElapsedTime < 1200) {
                mPlayer.position.x -= playerVelocity * time;
            } else if (mPlayerElapsedTime < 1400) {

            } else if (mPlayerElapsedTime < 1800) {
                mPlayer.position.x += playerVelocity * time;
            } else if (mPlayerElapsedTime < 2000) {

            } else if (mPlayerElapsedTime < 2200) {
                mPlayer.position.x -= playerVelocity * time;
            } else if (mPlayerElapsedTime < 2400) {

            } else if (mPlayerElapsedTime < 2600) {
                mPlayer.position.y -= playerVelocity * time;
            } else if (mPlayerElapsedTime < 2800) {

            } else if (mPlayerElapsedTime < 3100) {
                mPlayer.position.y += playerVelocity * time;
            } else if (mPlayerElapsedTime < 3300) {

            } else if (mPlayerElapsedTime < 3500) {
                mPlayer.position.y -= playerVelocity * time;
            } else if (mPlayerElapsedTime > 13000) {
                initLevel();
            }
        } else if (m_Selected == 1) {
            if (mPlayerElapsedTime < 1000) {

            } else if (mPlayerElapsedTime < 1100) {
                mPlayer.position.x -= playerVelocity * time;
            } else if (mPlayerElapsedTime < 2100) {
                
            } else if (mPlayerElapsedTime < 2205) {
                mPlayer.position.x += playerVelocity * time;
            }else if (mPlayerElapsedTime > 13000) {
                initLevel();
            }
        }
    }

    protected void checkPlayer(long time, int keys) {
        mPlayer.update(time);

        // check each weapon
        Vector weapons = mPlayer.getWeapons();
        for (int i = 0, n = weapons.size(); i < n; ++i) {
            final Weapon weapon = (Weapon)weapons.elementAt(i);

            weapon.update(time);
        }

        // move player with game restriction
        mPlayer.position.y -= mScrollSpeed * time;

        if (!mPlayer.isAlive()) {
            if (mPlayer.getLife() > 0)
                mParticleEmitters.addElement(ParticleEmitterFactory.customColorExplosionEmitter(mPlayer.position, 0xfb9f00));

            // respawn and reinitialize player position
            mPlayer.respawn();
            mPlayer.position.x  = (mDrawStartX + mDrawEndX) / 2;
            mPlayer.position.y  = mDrawStartY + 3 * StateManager.instance().getHeight() / 4;
        }
    }

	public void draw(Graphics g) {
		//Super
		if(m_DrawBG){
			g.drawImage(m_Background, 0, 0, Graphics.LEFT | Graphics.TOP);
			g.drawImage(StateManager.instance().getDarkImage(), 0, 0, Graphics.LEFT | Graphics.TOP);
			g.drawImage(m_Pages[m_Selected], StateManager.instance().getWidth()-5, StateManager.instance().getHeight()-5, Graphics.RIGHT | Graphics.BOTTOM);
		}
		m_DrawBG = false;
		
		//Draw
        switch (m_Selected) {
            case 0:
				super.draw(g);
				for(int i=0; i<6; ++i){
					g.drawImage(m_BattleTexts[i], StateManager.instance().getWidth()-4, 67+i*13, Graphics.RIGHT | Graphics.TOP);
				}
				break;
            case 1:
				super.draw(g);
				for(int i=0; i<3; ++i){
					g.drawImage(m_PowerUpTexts[i], StateManager.instance().getWidth()-4, 67+i*13, Graphics.RIGHT | Graphics.TOP);
				}
				for(int i=3; i<7; ++i){
					g.drawImage(m_PowerUpTexts[i], StateManager.instance().getWidth()-18, 67+i*13, Graphics.RIGHT | Graphics.TOP);
				}
				for(int i=3; i<6; ++i){
					g.drawImage(m_PowerUpsImg[i-3], StateManager.instance().getWidth()-4, 84+i*13, Graphics.RIGHT | Graphics.TOP);
				}
                break;
            case 2:
				g.drawImage(m_MissionFinishedImg, m_MissionFinishedImg.getWidth()/2 + 10, StateManager.instance().getHeight()/2, Graphics.HCENTER | Graphics.VCENTER);
				for(int i=0; i<8; ++i){
					g.drawImage(m_MoneyTexts[i], StateManager.instance().getWidth(), 67+i*13, Graphics.RIGHT | Graphics.TOP);
				}
                break;
            case 3:
				g.drawImage(m_GarageMenuImg, m_GarageMenuImg.getWidth()/2 + 10, StateManager.instance().getHeight()/2-m_GarageMenuImg.getWidth()/2+10, Graphics.HCENTER | Graphics.VCENTER);
				g.drawImage(m_GarageImg, m_GarageImg.getWidth()/2 + 10, StateManager.instance().getHeight()/2+m_GarageImg.getWidth()/2-10, Graphics.HCENTER | Graphics.VCENTER);
				for(int i=0; i<8; ++i){
					g.drawImage(m_GaragePartsTexts[i], StateManager.instance().getWidth(), 67+i*13, Graphics.RIGHT | Graphics.TOP);
				}
                break;
        }
		
		if(m_Drawn <=5) m_Drawn++;
	}

    protected void checkEndCondition(long time) {
        
    }

    private long    mPlayerElapsedTime;
	private boolean	m_DrawBG;

	//DATA
	protected int		m_Drawn;
	protected long		m_Time;
	protected Image[]	m_Pages;
	protected int		m_Selected;

	//Tutorial 1
	protected Image[]	m_BattleTexts, m_PowerUpTexts;

	//Tutorial 2
	protected Image		m_MissionFinishedImg;
	protected Image[]	m_MoneyTexts;

	//Tutorial 3
	protected Image		m_GarageMenuImg, m_GarageImg;
	protected Image[]	m_GaragePartsTexts, m_PowerUpsImg;

	private static final int TUTORIAL_STEPS = 4;
}
